function Site(data) {
    this.id = data.id;
    this.title = data.title? data.title : '';
}

function Url(data) {
    this.id = data.id;
    this.site_id = data.site_id;
    this.title = data.title? data.title : '';
}

function Msg(data) {
    this.id = data.id;
    this.site_id = data.site_id;
    this.title = data.title? data.title : '';
    this.content = data.content;
}

function DataProvider() {
    var self = this;

    self.onSitesUpdate = function(callback) {
        self.on_sites_update = callback;
    };

    self.onUrlsUpdate = function(callback) {
        self.on_urls_update = callback;
    };

    self.onMessagesLoad = function(callback) {
        self.on_messages_load = callback;
    };

    self.updateSites = function() {
        $.ajax({
            dataType: "json",
            url: 'http://pa.tst/site?format=json&callback=?',
            success: function(data) {
                if( data.error.code ) return;
                if( self.on_sites_update ) {
                    var sites = data.sites.map(function(site) {
                        return new Site(site);
                    });
                    self.on_sites_update(sites);
                }
            }
        });
    };

    self.updateUrls = function() {
        $.ajax({
            dataType: "json",
            url: 'http://pa.tst/url?format=json&callback=?',
            success: function(data) {
                if( data.error.code ) return;
                if( self.on_urls_update ) {
                    var sites = data.urls.map(function(url) {
                        return new Url(url);
                    });
                    self.on_urls_update(sites);
                }
            }
        });
    };

    self.loadMessages = function (url_id, id, limit) {
        $.ajax({
            dataType: "json",
            url: 'http://pa.tst/msg?format=json&callback=?',
            data: {url: url_id, id: id, limit: limit},
            success: function(data) {
                if( data.error.code ) return;
                if( self.on_messages_load ) {
                    var messages = data.messages.map(function(msg) {
                        return new Msg(msg);
                    });

                    self.on_messages_load(messages);
                }
            }
        });
    };
}

function AggViewModel() {
    var self = this;

    self.sites = ko.observableArray([]);
    self.urls = ko.observableArray([]);
    self.current_urls = ko.observableArray([]);
    self.messages = ko.observableArray([]);
    self.loaded_messages = ko.observableArray([]);
    self.data_provider = null;

    self.selectSite = function (site) {
        self.selectedSite(site);
        var cur_urls = self.urls().filter(function(url) {
            if( url.site_id === site.id ) return url;
        });
        self.current_urls(cur_urls);
        $.mobile.changePage($('#urls'));
        $('#urls ul').listview("refresh");
    };

    self.selectUrl = function (url) {
        self.selectedUrl(url);
        self.data_provider.loadMessages(url.id, 0, 5);
        $.mobile.changePage($('#msgs'));
        $('#msgs ul').listview("refresh");
    };

    self.msgLoaded = function(messages) {
        console.debug(messages);
        self.messages.push(messages);
    };

    self.selectedSite = ko.observable();
    self.selectedUrl = ko.observable();
    self.lastMsg = ko.observable();
}

$(document).ready(function(){
    var $vm = new AggViewModel();
    ko.applyBindings($vm);

    var dp = new DataProvider();
    dp.onSitesUpdate(function(sites) {
        $vm.sites(sites);
    });

    dp.onUrlsUpdate(function(urls) {
        $vm.urls(urls);
    });

    dp.onMessagesLoad($vm.msgLoaded);

    $vm.data_provider = dp;


    dp.updateSites();
    dp.updateUrls();
});

